package com.rdc.qingkou.model;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.entity.Result;
import com.rdc.qingkou.model.imodel.ICheckRegisteCallBack;
import com.rdc.qingkou.model.imodel.IRegisteCallBack;
import com.rdc.qingkou.retrofitService.ILoginRegisteService;
import com.rdc.qingkou.utils.MyUtils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;


public class RegisterModel {
    private static boolean isRegisted = false;
    private static boolean isLogin = false;
    private ILoginRegisteService iResult  = MyUtils.getRetrofitProvider("").create(ILoginRegisteService.class);


    public void isRegisted(String account, final ICheckRegisteCallBack callback) {
        iResult.isRegister(account)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result value) {
                        if (!value.getResult().equals("success")) {
                            callback.success();

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                          callback.fail("咦!网洛好像出了点问题！");

                    }

                    @Override
                    public void onComplete() {

                    }
                });




    }


    public void getConfirmCode(String telNumber, final IRegisteCallBack callBack) {
        iResult.getConfirmcode(telNumber).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<Result>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Response<Result> responseResult) {
//                        Headers headers = responseResult.headers();
//                        List<String> list = headers.values(("Set-Cookie"));
//                        if (list.size() == 0) {
//                            callBack.fail("cookie为空！");
//                            return;
//                        } else {
//                            MyApplication.setCookie(list.get(0));
//                        }
                        Result result = responseResult.body();
                        callBack.success(result.getCode());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void registe(String account, String password, String passwordAgain, String verificationCode, final IRegisteCallBack callBack) {
        iResult.register(account, password, passwordAgain, verificationCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result value) {
                        MyApplication.setCookie("");
                        if (value.getResult().equals("success")) {
                            callBack.success("");

                        }else {
                            callBack.fail(value.getReason());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.fail("咦!网洛好像出了点问题！");

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void changePassword(String account,String newPassword,String passwordAgain,String verificationCode,final IRegisteCallBack callBack){
        iResult.changePassword(account,newPassword,passwordAgain,verificationCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result value) {
                        if (value.getResult().equals("success")) {
                            callBack.success("");


                        }else {
                            callBack.fail(value.getReason());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                        callBack.fail("咦!网洛好像出了点问题！");

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}




